package at.codejedi.sto.gui.combat;

import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTabbedPane;

import at.codejedi.sto.config.Configuration;
import at.codejedi.sto.gui.detail.chart.BattleDetailChart;
import at.codejedi.sto.gui.detail.chart.BattleDetailChartData;
import at.codejedi.sto.gui.lazy.LazyPanel;
import at.codejedi.sto.model.combat.Entry;
import at.codejedi.sto.model.combat.PlayerBattle;

public class BattleDetailView extends LazyPanel implements IDetailView {

	private static final long serialVersionUID = 8864832614515543665L;

	BattleDetailChart chart;
	EntryView entryView;

	BattleStatisticsPanel statisticsPanel;

	public BattleDetailView() {
		setLayout(new GridLayout(0, 1, 0, 0));
		JTabbedPane tabbedPane = new JTabbedPane();
		add(tabbedPane);

		chart = new BattleDetailChart("Battle Details");
		tabbedPane
				.addTab(Configuration
						.getLabel("BattleStatistics.Tabs.Overview"), chart);

		statisticsPanel = new BattleStatisticsPanel();
		tabbedPane.addTab(
				Configuration.getLabel("BattleStatistics.Tabs.Statistics"),
				statisticsPanel);

		entryView = new EntryView();
		tabbedPane.addTab(
				Configuration.getLabel("BattleStatistics.Tabs.RawData"),
				entryView);

	}

	private void addDamage(BattleDetailChartData netBattleDetailChartData,
			BattleDetailChartData rawBattleDetailChartData, List<Entry> damage,
			long startMillis) {
		for (Entry e : damage) {
			long time = e.getTime().getTimeInMillis() - startMillis;
			double netDamage = Math.abs(e.getNetDamage());
			netBattleDetailChartData.damage.put(time, netDamage);
			double rawDamage = Math.abs(e.getRawDamage());
			rawBattleDetailChartData.damage.put(time, rawDamage);
		}
	}

	public void displayBattle(PlayerBattle b) {
		statisticsPanel.setStatistics(b);

		List<BattleDetailChartData> dealt = new ArrayList<BattleDetailChartData>();

		BattleDetailChartData netDealt = new BattleDetailChartData();
		BattleDetailChartData rawDealt = new BattleDetailChartData();

		netDealt.name = "net dealt";
		rawDealt.name = "raw dealt";

		dealt.add(netDealt);
		dealt.add(rawDealt);

		addDamage(netDealt, rawDealt, b.getDamageDealt(),
				b.getBeginningMillis());

		BattleDetailChartData netReceived = new BattleDetailChartData();
		BattleDetailChartData rawReceived = new BattleDetailChartData();

		netReceived.name = "net received";
		rawReceived.name = "raw received";

		dealt.add(netReceived);
		dealt.add(rawReceived);

		addDamage(netReceived, rawReceived, b.getDamageReceived(),
				b.getBeginningMillis());

		chart.init(dealt);
		chart.setVisible(false);
		chart.setVisible(true);

		entryView.display(b);
	}

}
